package com.joint.base.bean.json;

import com.fz.us.base.util.GsonUtil;
import com.fz.us.base.util.LogUtil;
import com.google.gson.*;
import com.joint.base.entity.BusinessConfig;
import com.joint.base.service.CommonConfigService;
import com.joint.base.service.CompanyService;
import com.joint.base.service.UsersService;
import com.joint.base.util.DataUtil;
import com.joint.base.util.SpringUtils;
import com.joint.base.util.StringUtils;

import java.lang.reflect.Type;

/**
 * Created by hpj on 2015/4/2.
 */
public class BusinessConfigGsonAdapter implements JsonSerializer<BusinessConfig>, JsonDeserializer<BusinessConfig> {


    @Override
    public BusinessConfig deserialize(JsonElement json, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {

        CompanyService companyService = SpringUtils.getBean(CompanyService.class);
        UsersService usersService = SpringUtils.getBean(UsersService.class);
        CommonConfigService commonConfigService = SpringUtils.getBean(CommonConfigService.class);

        JsonObject o = json.getAsJsonObject();
        BusinessConfig bcfg = new BusinessConfig();

        LogUtil.info(GsonUtil.getString(o, "company"));
        LogUtil.info("{company}"+companyService.get(GsonUtil.getString(o, "company")).getAdminId());

        bcfg.setCompany(companyService.get(GsonUtil.getString(o, "company")));
        bcfg.setCreater(usersService.get(GsonUtil.getString(o, "creater")));
        if(StringUtils.isNotEmpty(GsonUtil.getString(o,"createConfig"))){
            bcfg.setCreateConfig(commonConfigService.get(GsonUtil.getString(o,"createConfig")));
        }
        if(StringUtils.isNotEmpty(GsonUtil.getString(o, "docConfig"))){
            bcfg.setDocConfig(commonConfigService.get(GsonUtil.getString(o, "docConfig")));
        }
        if(StringUtils.isNotEmpty(GsonUtil.getString(o,"eidtConfig"))){
            bcfg.setEditConfig(commonConfigService.get(GsonUtil.getString(o, "eidtConfig")));
        }
        if(StringUtils.isNotEmpty(GsonUtil.getString(o,"readConfig"))){
            bcfg.setReadConfig(commonConfigService.get(GsonUtil.getString(o, "readConfig")));
        }
        bcfg.setOtype(GsonUtil.getInteger(o, "otype"));
        bcfg.setPtype(GsonUtil.getInteger(o, "ptype"));
        bcfg.setVersion(GsonUtil.getInteger(o, "version"));
        bcfg.setProcessDefinitionId(GsonUtil.getString(o, "processDefintionId"));
        bcfg.setTriggleTime(DataUtil.StringToDate(GsonUtil.getString(o, "triggleTime")));

        return bcfg;
    }

    @Override
    public JsonElement serialize(BusinessConfig businessConfig, Type type, JsonSerializationContext jsonSerializationContext) {
        JsonObject json = new JsonObject();
        json.addProperty("otype",businessConfig.getOtype());
        json.addProperty("ptype",businessConfig.getPtype());
        json.addProperty("version",businessConfig.getVersion());
        json.addProperty("createConfig",businessConfig.getCreateConfig()!=null?businessConfig.getCreateConfig().getId():null);
        json.addProperty("docConfig",businessConfig.getDocConfig()!=null?businessConfig.getDocConfig().getId():null);
        json.addProperty("eidtConfig",businessConfig.getEditConfig()!=null?businessConfig.getEditConfig().getId():null);
        json.addProperty("readConfig",businessConfig.getReadConfig()!=null?businessConfig.getReadConfig().getId():null);
        json.addProperty("processDefintionId",businessConfig.getProcessDefinitionId());
        json.addProperty("triggleTime", DataUtil.DateToString(businessConfig.getTriggleTime()));
        json.addProperty("company",businessConfig.getCompany().getId());
        json.addProperty("creater",businessConfig.getCreater().getId());
        return json;
    }
}
